use crate::gen_title;

pub fn ch2_9_1() {
    gen_title("ch2_9_1", 18);

    let tup: (i32, f64, u8) = (500, 6.4, 1);

    println!("The value of tup is: {:?}", tup);
}

pub fn ch2_9_2() {
    gen_title("ch2_9_2", 18);

    let tup = (500, 6.4, 1);
    let (x, y, z) = tup;
    println!("The value of y is: {}", y);
    println!("The value of z is: {}", z);
    println!("The value of x is: {}", x);
}

pub fn ch2_9_3() {
    gen_title("ch2_9_3", 18);

    let x: (i32, f64, u8) = (500, 6.4, 1);
    let five_hundred = x.0;
    let six_point_four = x.1;
    let one = x.2;
    println!("The value of one is: {}", one);
    println!("The value of six_point_four is: {}", six_point_four);
    println!("The value of five_hundred is: {}", five_hundred);
}

pub fn ch2_9_4() {
    gen_title("ch2_9_4", 18);

    let s1 = String::from("hello");
    let (s2, len) = calculate_length(s1);

    println!("The length of '{}' is {}.", s2, len);

    fn calculate_length(s: String) -> (String, usize) {
        let length = s.len();
        (s, length)
    }
}

pub fn ch2_9_pra_1() {
    gen_title("ch2_9_pra_1", 18);

    let _t0: (u8, i16) = (0, -1);
    let _t1: (u8, (i16, u32)) = (0, (-1, 1));
    let t: (u8, u16, i64, &str, String) = (1u8, 2u16, 3i64, "hello", String::from(", world"));

    println!("{:?}", _t0);
    println!("{:?}", _t1);
    println!("{:?}", t);
}

pub fn ch2_9_pra_2() {
    gen_title("ch2_9_pra_2", 18);

    let t = ("i", "am", "sunface");
    assert_eq!(t.2, "sunface");
    println!("{:?}", t);
}

pub fn ch2_9_pra_3() {
    gen_title("ch2_9_pra_3", 18);

    let too_long_tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
    println!("too long tuple: {:?}", too_long_tuple);
}

pub fn ch2_9_pra_4() {
    gen_title("ch2_9_pra_4", 18);

    let tup = (1, 6.4, "hello");

    let (x, y, z) = tup;
    assert_eq!(x, 1);
    assert_eq!(y, 6.4);
    assert_eq!(z, "hello");
    println!("x: {}, y: {}, z: {}", x, y, z);
}

pub fn ch2_9_pra_5() {
    gen_title("ch2_9_pra_5", 18);

    let (x, y, z);

    (x, y, z) = (1, 2, 3);

    assert_eq!(x, 1);
    assert_eq!(y, 2);
    assert_eq!(z, 3);
    println!("x: {}, y: {}, z: {}", x, y, z);
}

pub fn ch2_9_pra_6() {
    gen_title("ch2_9_pra_6", 18);

    let (x, y) = sum_multiply((2, 3));
    assert_eq!(x, 5);
    assert_eq!(y, 6);

    println!("x: {}, y: {}", x, y);

    fn sum_multiply(nums: (i32, i32)) -> (i32, i32) {
        (nums.0 + nums.1, nums.0 * nums.1)
    }
}
